package com.volvo.caretrack.mobile.actions;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.volvo.caretrack.mobile.model.error.ErrorData;
import com.volvo.caretrack.mobile.util.CookieUtil;
import com.volvo.caretrack.mobile.util.ParamUtil;


public abstract class BaseAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		ActionForward forward = null;
		
		Map<?, ?> params = request.getParameterMap(); 
		String callback = ParamUtil.getParam(params, "callback");
		String sessionID = CookieUtil.getValue(request, "sessionID");
				
		try{
			Object data = loadData(sessionID, params);
			request.setAttribute("data", data);
			request.setAttribute("callback", callback); // only really for JSON feeds (for Brian S)
			forward = mapping.findForward("json");
		}catch(Exception e){
			ErrorData data = new ErrorData();
			data.setTitle(e.getMessage());
			request.setAttribute("data", data);
			forward = mapping.findForward("error");
		}
	
		return forward;
	}
	
	public abstract Object loadData(String sessionID, Map params) throws Exception;
	
}
